const {query} = require('./dao')

// 插入
function insert(message) {
    const sql = 'insert into message (name,content) values (?,?)'
    const data = [message.name, message.content];
    return query(sql, data);
}

//查询、显示、分页
function select({name = '', content = '', pageNum = 1, pageSize = 5}) {
    const sql = 'select * from message where name like ? and content like ? order by id  desc limit ?,?';
    const data = ['%' + name + '%', '%' + content + '%', (pageNum - 1) * pageSize, pageSize];
    return query(sql, data);
}

//查询总数的操纵
function count({name = '', content = ''}) {
    const sql = 'select count (*) as total from message where name like ? and content like ?'
    const data = ['%' + name + '%', '%' + content + '%'];
    return query(sql, data);
}

//删除
function deleteMessage(id) {
    const sql = 'delete  from message where id = ? '
    const data = [id];
    return query(sql, data);
}


module.exports = {
    insert,
    select,
    deleteMessage,
    count
}